﻿namespace WMS6.Web.Common
{
    using System;
    using System.Data;
    using System.Linq;
    using System.Text;
    using WMS6.Web;

    public class SearchDateAjax : BasePage
    {
        private void AjaxResponse(string StrHtml)
        {
            base.Response.Clear();
            base.Response.StatusCode = 200;
            base.Response.StatusDescription = "OK";
            base.Response.ContentType = "text/html";
            base.Response.Write(StrHtml);
            base.Response.Flush();
            base.Response.End();
            base.Response.Close();
        }

        public void DataCounvert(DataRow[] ds)
        {
            StringBuilder StrHtml = new StringBuilder();
            if ((ds != null) && (ds.Count<DataRow>() > 0))
            {
                string Size = string.Empty;
                int iCount = 10;
                Size = base.GetValueFromAppSettings("SEARCH_KEY");
                if (string.IsNullOrEmpty(Size))
                {
                    Size = "10";
                }
                else
                {
                    iCount = Convert.ToInt32(Size);
                }
                if (ds.Count<DataRow>() < iCount)
                {
                    iCount = ds.Count<DataRow>();
                    Size = iCount.ToString();
                }
                StrHtml.AppendFormat("<select id=\"Select1\" multiple=\"multiple\" onchange=\"SetValue(this);\" size=\"{0}\" style=\"width:100%\">", Size, base.Request.QueryString["id"]);
                for (int i = 0; iCount > i; i++)
                {
                    StrHtml.AppendFormat("<option value=\"{0}\" title=\"{1}\"  >{0}</option>", ds[i][0].ToString(), ds[i][1].ToString());
                }
                StrHtml.Append("</select>");
            }
            this.AjaxResponse(StrHtml.ToString());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string strkey = base.Request.QueryString["key"];
            string strkey2 = base.Request.QueryString["key2"];
            string strtype = base.Request.QueryString["type"];
            string strstatus = base.Request.QueryString["status"];
            if (!string.IsNullOrEmpty(strkey) && !string.IsNullOrEmpty(strtype))
            {
                if (strstatus == "NEW")
                {
                    this.Session["SearchData"] = null;
                }
                this.SearchData(strkey, strkey2, strtype);
            }
        }

        public void SearchData(string key, string key2, string type)
        {
            DataSet ds = null;
            if (this.Session["SearchData"] == null)
            {
                ds = CommoneMethodServiceClientProxy.GetInstance().SearchDataByKey(key, key2, type);
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    this.Session["SearchData"] = ds;
                }
            }
            else
            {
                ds = this.Session["SearchData"] as DataSet;
            }
            string strWhere = string.Empty;
            string CS40001 = type;
            if (CS40001 != null)
            {
                if (!(CS40001 == "SKU"))
                {
                    if (CS40001 == "LOC")
                    {
                        strWhere = " LOC LIKE '" + key.Replace("*", "[*]").Replace("-", "[-]") + "%'";
                    }
                }
                else
                {
                    strWhere = " SKU_ID LIKE '" + key.Replace("*", "[*]").Replace("-", "[-]") + "%'";
                }
            }
            this.DataCounvert(ds.Tables[0].Select(strWhere));
        }
    }
}

